package com.sky.mapper;

import com.sky.entity.AddressBook;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @InterfaceName: AddressBookMapper
 * @Author: YuanDing
 * @Date: 2024/2/7 11:57
 * @Description:
 */

@Mapper
public interface AddressBookMapper {
    /**
     * 根据用户名查询全部地址
     * @param userId
     * @return
     */
    @Select("select * from address_book where user_id = #{userId}")
    List<AddressBook> selectAllByUserId(Long userId);

    /**
     * 获取用户默认地址
     * @return
     */
    @Select("select * from address_book where user_id = #{userId} and is_default = 1")
    AddressBook selectUserDefaultAddress(Long userId);

    /**
     * 新增一条地址
     * @param addressBook
     */
    void insertAddress(AddressBook addressBook);

    /**
     * 设为默认地址
     * @param addressBook
     */
    @Update("update address_book set is_default = 1 where id = #{id}")
    void updateDefaultAddress(AddressBook addressBook);

    /**
     * 取消默认地址
     * @param id
     */
    @Update("update address_book set is_default = 0 where id = #{id}")
    void updateDefaultAddressById(Long id);

    /**
     * 根据id 查地址
     * @param id
     * @return
     */
    @Select("select * from address_book where id = #{id}")
    AddressBook selectAddressById(Long id);

    /**
     * 更新地址
     * @param addressBook
     */
    void updateAddress(AddressBook addressBook);

    /**
     * 根据id删除一条地址
     * @param id
     */
    @Delete("delete from address_book where id = #{id}")
    void deleteById(Long id);
}
